package com.system.employee.web;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.system.common.util.ExtAjaxForm;
import com.system.common.util.ExtAjaxResponse;
import com.system.common.util.ExtPageable;
import com.system.employee.entity.Employee;
import com.system.employee.entity.dto.EmployeeDTO;
import com.system.employee.service.IEmployeeService;
import com.system.user.entity.User;
import com.system.user.service.IUserService;





@Controller
@RequestMapping("/employee")
public class EmployeeController 
{
	@Autowired
	private IEmployeeService employeeService;
	@Autowired
	private IUserService userService;

	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Employee employee) 
	{	
		
			try {
				employeeService.saveOrUpdate(employee);
				return new ExtAjaxResponse(true,"更新员工信息成功！");
			} catch (Exception e) {
				System.out.println(e);
				return new ExtAjaxResponse(false,"更新员工信息失败！");
			}
		
		
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long empId) 
	{
		try {
			Employee Employee =  employeeService.findOne(empId);
			if(null!=Employee) {
				employeeService.delete(Employee);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			System.out.println(e);
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	@RequestMapping("/deleteEmployees")
	public @ResponseBody ExtAjaxResponse deleteEmployees(@RequestParam Long[] ids) 
	{
		try {
			List<Long> idLists = Arrays.asList(ids);
			if(null!=idLists) {
				employeeService.delete(idLists);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	
	@RequestMapping("/findOne")
	public @ResponseBody Employee findOne(@RequestParam Long empId) 
	{
		Employee employee =  employeeService.findOne(empId);
		
		return employee;
	}
	@RequestMapping("/test")
	public @ResponseBody ExtAjaxForm test(@RequestParam String id) 
	{
		User user =  userService.findOne(id);
		Employee employee = employeeService.findUser(user);
		
		
		return new ExtAjaxForm(true,employee);
	}
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Employee> findAll() 
	{
		 List<Employee> lists =  employeeService.findAll();
		
		return lists;
	}
	@RequestMapping("/findEmployee")
	public @ResponseBody List<Employee> findEmployee(@RequestParam String empName ) 
	{
		//System.out.println(empName+"查询条件");
		List<Employee> list =   employeeService.findByEmpNameLike("%"+empName+"%");
		
		return list;
	}
	
	@RequestMapping("/findPage")
	public @ResponseBody Page<Employee> findPage(EmployeeDTO employeeDTO,ExtPageable pageable) 
	{
		if(pageable.getSort()=="id")
		   pageable.setSort("empId");
		Page<Employee> page =  employeeService.findAll(EmployeeDTO.getSpecification(employeeDTO), pageable.getPageable());
		
		return page;
	}
	

	@RequestMapping("/saveIdcardImg")   
	public @ResponseBody ExtAjaxResponse saveIdcardImg(@RequestParam Long empId, HttpServletRequest request,HttpServletResponse response) throws IOException {  
        //创建一个通用的多部分解析器  
	   String fileName="";
	   Employee employee=employeeService.findOne(empId);
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
       
      try{
    	  System.out.println("进入try"); 
    	  //判断 request 是否有文件上传,即多部分请求  
        if(multipartResolver.isMultipart(request)){  
        	System.out.println("进入if"); 
            //转换成多部分request    
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
            //取得request中的所有文件名  
            Iterator<String> iter = multiRequest.getFileNames();  
            while(iter.hasNext()){  
            	System.out.println("进入while"); 
                //记录上传过程起始时的时间，用来计算上传时间  
                int pre = (int) System.currentTimeMillis();  
                //取得上传文件  
                MultipartFile file = multiRequest.getFile(iter.next());  
                if(file != null){  
                    //取得当前上传文件的文件名称  
                    String myFileName = file.getOriginalFilename();  
                    System.out.println(myFileName+"文件存在");  
                    //如果名称不为“”,说明该文件存在，否则说明该文件不存在  
                    if(myFileName.trim() !=""){  
                        System.out.println(myFileName);  
                        //重命名上传后的文件名  
                         fileName = employee.getEmpName() + file.getOriginalFilename();  
                        //定义上传路径  
                        String path = "D:\\soft\\eclipse-jee-oxygen-R-win32\\eclipse-workspace\\ssh-demo\\src\\main\\webapp\\resources\\images\\user-profile\\" + fileName;  
                        File localFile = new File(path);  
                        file.transferTo(localFile);  
                    }  
                }  
                //记录上传该文件后的时间  
                int finaltime = (int) System.currentTimeMillis();  
                System.out.println(finaltime - pre);  
            }      
          } 
           employee.setIdcardImg("resources\\images\\user-profile\\"+fileName);
           employeeService.saveOrUpdate(employee);
        	return new ExtAjaxResponse(true,"操作成功！");  
        }catch (Exception e) {
			System.out.println(e);
			return new ExtAjaxResponse(false,"操作失败！");
		}
       
        
    }  
	
}
